﻿using System;
using System.Collections.Generic;
using System.Linq;
using MDT_MonitorModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// Pour en savoir plus sur le modèle d'élément Page Détail du groupe, consultez la page http://go.microsoft.com/fwlink/?LinkId=234229

namespace MDT_Metro_monitoring
{
    /// <summary>
    /// Page affichant une vue d'ensemble d'un groupe, ainsi qu'un aperçu des éléments
    /// qu'il contient.
    /// </summary>
    public sealed partial class ServerDetailPage : MDT_Metro_monitoring.Common.LayoutAwarePage
    {
        private DispatcherTimer updateTimer;
        
        public ServerDetailPage()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Populates the page with content passed during navigation.  Any saved state is also
        /// provided when recreating a page from a prior session.
        /// </summary>
        /// <param name="navigationParameter">The parameter value passed to
        /// <see cref="Frame.Navigate(Type, Object)"/> when this page was initially requested.
        /// </param>
        /// <param name="pageState">A dictionary of state preserved by this page during an earlier
        /// session.  This will be null the first time a page is visited.</param>
        protected override void LoadState(Object navigationParameter, Dictionary<String, Object> pageState)
        {
            // TODO: affectez un groupe pouvant être lié à this.DefaultViewModel["Group"]
            // TODO: affectez une collection d'éléments pouvant être liés à this.DefaultViewModel["Items"]
            // Allow saved page state to override the initial item to display
            if (pageState != null && pageState.ContainsKey("SelectedItem"))
            {
                navigationParameter = pageState["SelectedItem"];
            }

            // TODO: Create an appropriate data model for your problem domain to replace the sample data
            var srv = (Server)navigationParameter;

            this.DefaultViewModel["Server"] = srv;

            this.pageTitle.Text = srv.Name;
            if (srv.Clients != null)
            {
                var items = from client in srv.Clients orderby client.sort1 descending, client.sort2 descending, client.sort3 select client;
                this.DefaultViewModel["Items"] = items;
            }

            updateTimer = new DispatcherTimer();
            updateTimer.Interval = new TimeSpan(0, 0, 10);
            updateTimer.Tick += UpdateUI;
            updateTimer.Start();
        }

        private void UpdateUI(object sender, object e)
        {
            if (App.updateAvailable)
            {

                if (((Server)this.DefaultViewModel["Server"]).Clients != null)
                {
                    var items = from client in ((Server)this.DefaultViewModel["Server"]).Clients orderby client.sort1 descending, client.sort2 descending, client.sort3 select client;
                    this.DefaultViewModel["Items"] = items;
                }
            }
        }

        private void itemGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var item = (Computer)e.ClickedItem;
            this.Frame.Navigate(typeof(ItemDetailPage), item);
        }
    }
}
